
//
// 110410 - AH - (Fantoft) Created. 
//

package org.soekrat.engine.pitbul.generator;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.pitbul.parser.PitbulParser;
import org.soekrat.engine.util.CommandLineParser;

/**
 * <p>
 *   Utility methods for generating Pitbul code.
 * </p>
 * @author Arne Halvorsen (AH)
 */
public final class PitbulGenerator 
{
  private PitbulGenerator(){} // Utility methods 
  
  private static Map<String, FormGenerator> c_generators=
    new HashMap<String, FormGenerator>();
  
  /**
   * <p>
   *   Adds a 
   *   {@link FormGenerator}.
   * </p>
   * @param fg {@code FormGenerator}.
   */
  private synchronized static void addFormGenerator(FormGenerator fg)
  {
    if (fg==null)
    {
      throw new NullPointerException("fg");
    }
    
    c_generators.put(fg.getForm(), fg);
  }
  
  static
  {
    addFormGenerator(new NodeFormGenerator());
    addFormGenerator(new ResourceFormGenerator());
  }
  
  private static synchronized FormGenerator[] getGenerators()
  {
    return c_generators.values().toArray(FormGenerator.EMPTY_ARRAY);
  }
  
  /**
   * <p>
   *   Generates Pitbul code.
   * </p>
   * @param se {@link Soekrat2Engine}.
   * @param f  Represents file to write code to.
   * @throws IOException If fails.
   */
  public static void generate(Soekrat2Engine se, File f) throws IOException
  {
    if (se==null)
    {
      throw new NullPointerException("se");
    }
    if (f==null)
    {
      throw new NullPointerException("f");
    }
    
    PrintWriter pw=null;
    try
    {
      pw=new PrintWriter(f);
      generate(se, pw);
    }
    finally
    {
      if (pw!=null) pw.close();
    }
  }
  
  /**
   * <p>
   *   Generates Pitbul code.
   * </p>
   * @param se {@link Soekrat2Engine}.
   * @param pw Stream to write code to.
   * @throws IOException If fails.
   */
  public static void generate(Soekrat2Engine se, PrintWriter pw) 
    throws IOException
  {
    if (se==null)
    {
      throw new NullPointerException("se");
    }
    if (pw==null)
    {
      throw new NullPointerException("pw");
    }
    
    FormGenerator[] generators=getGenerators();
    for (FormGenerator curr : generators) curr.generate(se, pw);    
  }
  
  /**
   * <p>
   * </p>
   * @param args
   */
  public static void main(String[] args) 
  {
    try
    {
      CommandLineParser clp=new CommandLineParser(args);
      
      File inf=clp.mandatoryFileValue("-in");
      File outf=clp.fileValue("-out", new File("./out.ptb"));
      
      Soekrat2Engine se=new Soekrat2Engine();
      
      PitbulParser.parse(se, inf);
      PitbulGenerator.generate(se, outf);
      
      System.exit(0);
    }
    catch (IOException iox)
    {
      iox.printStackTrace();
      System.exit(-1);
    }
  }

}
